<template>
  <div>
    <a-modal
      width="80%"
      :title="title"
      :maskClosable="false"
       :destroyOnClose="true"
      :visible.sync="editModalVisible"
      @cancel="handleCancleDbSync"
    >
    <template slot="footer">
        <a-button @click="handleCancleDbSync">
          关闭
        </a-button>
        <a-button type="primary" v-if="!disableSubmit" :loading="syncLoading" @click="handleDbSync">保存</a-button>
        <a-button type="primary" v-if="isCheck" :loading="syncLoading" @click="handleCheck('1')">审核不通过</a-button>
        <a-button type="primary" v-if="isCheck" :loading="syncLoading" @click="handleCheck('3')">审核通过</a-button>
      </template>
      <div class="table-page-search-wrapper warp">
        <a-form-model layout="inline" labelAlign="right" ref="ruleForm" :model="formData" :rules="rules">
          <a-row :gutter="24">
            <a-col :xl="8" :lg="8" :md="8" :sm="24" >
              <a-form-model-item label="任务名称" prop="taskName">
                <a-input :disabled="true" v-model="formData.taskName" placeholder="请输入任务名称"/>
              </a-form-model-item>
            </a-col>
            <a-col :xl="8" :lg="8" :md="8" :sm="24" >
              <a-form-model-item label="任务编码" prop="taskCode">
                <a-input :disabled="true" v-model="formData.taskCode" placeholder="请输入任务编码"/>
              </a-form-model-item>
            </a-col>
            <a-col :xl="8" :lg="8" :md="8" :sm="24">
              <a-form-model-item label="任务分级">
                <j-dict-select-tag
                  :disabled="true"
                  v-model="formData.taskLevel"
                  placeholder="请选择任务级别"
                  dict-code="pms_task_level"
                />
              </a-form-model-item>
            </a-col>
            <a-col :xl="8" :lg="8" :md="8" :sm="24">
              <a-form-model-item label="时间" prop="time">
                <a-range-picker :disabled="true" v-model="formData.time"/>
              </a-form-model-item>
            </a-col>
            <a-col :xl="8" :lg="8" :md="8" :sm="24">
              <a-form-model-item label="负责人" prop="taskDirector">
                <j-select-multi-user :disabled="true" :multiple="false" v-model="formData.taskDirector" ></j-select-multi-user>
              </a-form-model-item>
            </a-col>
            <a-col :xl="8" :lg="8" :md="8" :sm="24">
              <a-form-model-item label="审核人">
                <j-select-multi-user :disabled="true" :multiple="false" v-model="formData.taskReviewer" ></j-select-multi-user>
              </a-form-model-item>
            </a-col>
            <a-col :xl="8" :lg="8" :md="8" :sm="24">
              <a-form-model-item label="抄送人">
                <j-select-multi-user :disabled="true" v-model="formData.taskCclist"></j-select-multi-user>
              </a-form-model-item>
            </a-col>
            <a-col :xl="8" :lg="8" :md="8" :sm="24" >
              <a-form-model-item label="前置任务">
                <a-input :disabled="true" v-model="formData.preTaskCode" placeholder="请输入前置任务编码"/>
              </a-form-model-item>
            </a-col>
            <a-col :xl="8" :lg="8" :md="8" :sm="24" v-if="formData.taskStatus !== '0'">
              <a-form-model-item label="完成比">
                <a-slider :disabled="disableSubmit || formData.hasChildren === '1'" :max="100" :min="0" v-model="formData.taskProgress" />
              </a-form-model-item>
            </a-col>
            <a-col :xl="16" :lg="16" :md="16" :sm="24">
              <a-form-model-item label="备注">
                <a-textarea :disabled="disableSubmit" v-model="formData.remarks" :rows="1" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
        <a-tabs defaultActiveKey="2">
          <a-tab-pane key="2" tab="任务详情" forceRender>
            <j-editor v-if="jEditorVisible" :disabled="disableSubmit" v-model="formData.taskDesc"/>
          </a-tab-pane>
          <a-tab-pane key="3" tab="操作记录">
            <projectlog :param="{taskCode:formData.taskCode}"></projectlog>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-modal>
  </div>
</template>
<script>
import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUser'
import JTreeSelect from '@/components/jeecg/JTreeSelect'
import moment from 'moment'
import { postAction, putAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JEditor from '@/components/jeecg/JEditor'
import Projectlog from '../modules/Projectlog'
export default {
  name: 'MyProjectListModal',
  components: {
    JSelectMultiUser,
    JTreeSelect,
    JEditor,
    Projectlog
  },
  props: ['projectCode', 'type'],
  data() {
    return {
      editModalVisible: false,
      jEditorVisible:false,
      disableSubmit: false,
      isCheck: false,
      title: '新增',
      rules: {
        taskName: [
          { required: true, message: '请输入任务名称', trigger: 'blur' }
        ],
        taskCode: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
          { validator: (rule, value, callback) => validateDuplicateValue('pms_task', 'task_code', value, this.formData.id, callback) }
        ],
        time: [
          { required: true, message: '请选择时间', trigger: 'blur' }
        ],
        taskDirector: [
          { required: true, message: '请选择负责人', trigger: 'blur' }
        ],
        antecedents: [
          { required: true, message: '请选择所属项目', trigger: 'blur' }
        ]
      },
      formData: {},
      addFormData: {
        taskProgress: 0,
        taskStatus: '0'
      },
      syncLoading: false,
      url: {
        add: '/pms/task/add',
        edit: '/pms/task/edit',
        addTaskList: '/pms/taskTime/add',
        editTaskList: '/pms/taskTime/edit',
        queryByCode: '/pms/task/queryByCode'
      },
      dateFormat: 'YYYY-MM-DD'
    }
  },
  computed: {

  },
  methods: {
    handleCancleDbSync() {
      this.isCheck = false
      this.disableSubmit = false
      this.$refs['ruleForm'].resetFields()
      this.editModalVisible = false
      this.jEditorVisible = false
    },
    add() {
      this.formData = { ...this.addFormData }
      this.editModalVisible = true
      this.$nextTick(() => {
        this.jEditorVisible = true
      })
    },
    edit(data) {
      const obj = { ...data }
      obj.time = []
      this.formData = obj
      if (this.formData.startTime && this.formData.endTime) {
        this.formData.time = [moment(this.formData.startTime), moment(this.formData.endTime)]
      }
      this.editModalVisible = true
      this.$nextTick(() => {
        this.jEditorVisible = true
      })
    },
    addChild(data) {
      this.formData = { ...this.addFormData }
      this.formData.parentTaskCode = data.taskCode
      this.editModalVisible = true
    },
    async handleDbSync() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          this.updateData()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    updateData() {
      this.formData.startTime = moment(this.formData.time[0]).format(this.dateFormat)
      this.formData.endTime = moment(this.formData.time[1]).format(this.dateFormat)
      this.formData.projectCode = this.projectCode
      if (this.formData.taskProgress === 100 && !this.checkChildrenStatus()) {
        this.$message.warning('所属子任务未完成，不能完成本任务')
        return false
      }
      this.setTaskStatus()
      if (this.formData.id) {
        putAction(this.url.edit, this.formData).then(res => {
          this.actionSuccess(res)
        })
      } else {
        postAction(this.url.add, this.formData).then(res => {
          this.actionSuccess(res)
        })
      }
    },
    checkChildrenStatus() { // 保存之前判断子任务是否全部完成
      let flag = true
      console.log(this.formData)
      if (this.formData.children && this.formData.children.length > 0) {
        this.formData.children.forEach(item => {
          if (item.taskStatus !== '3') {
            flag = false
            return false
          }
        })
      }
      return flag
    },
    setTaskStatus() { // 根据进度设置单据状态
      const val = this.formData.taskProgress
      if (this.formData.taskStatus === '0') {
        return false
      }
      if (val < 100) {
        this.formData.taskStatus = '1'
      } else {
        if (this.formData.taskReviewer) {
          this.formData.taskStatus = '2'
        } else {
          this.formData.taskStatus = '3'
        }
      }
    },
    actionSuccess(res) {
      if (res.success) {
        this.$emit('ok')
        this.editModalVisible = false
      } else {
        const massage = res.message.indexOf('StackOverflowError') >= 0 ? '前置任务设置失败' : res.message
        this.$message.warning(massage)
      }
    },
    handleCheck(status) {
      if (status === '1') {
        this.formData.taskProgress = 0
      }
      this.formData.taskStatus = status
      putAction(this.url.edit, this.formData).then(res => {
        this.actionSuccess(res)
      })
    }
    // startTask() {
    //   if (!this.formData.preTaskCode) {
    //     this.formData.taskStatus = '1'
    //     return false
    //   }
    //   getAction(this.url.queryByCode, {
    //     code: this.formData.preTaskCode
    //   }).then(res => {
    //     if (res.success) {
    //       if (res.result.taskStatus === '3') {
    //         this.formData.taskStatus = '1'
    //       } else {
    //         this.$message.warning('前置任务位完成，不允许开始')
    //       }
    //     } else {
    //       this.$message.warning(res.message)
    //     }
    //   })
    // }
  }
}
</script>
<style lang="less">

</style>
